package com.cy.ppj.web.admin.mall;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.mall.GoodsCategoryAddAO;
import com.cy.ppj.model.ao.mall.GoodsCategoryEditAO;
import com.cy.ppj.model.domian.mall.Brand;
import com.cy.ppj.model.domian.mall.GoodsCategory;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.GoodsCategoryAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsCategoryAdminListVO;
import com.cy.ppj.service.mall.GoodsCategoryService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 后台-商品类别管理
 * @Author tanyilin
 * @Date 2019/7/3 14:49
 */
@RestController
@RequestMapping("/admin/mall/goods/category")
@Api(tags = "后台-商品类别管理")
public class AdminGoodsCategoryController extends BaseController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @ApiOperation("类别列表")
    @GetMapping("/list")
    public PageBean<GoodsCategoryAdminListVO> getList(GoodsCategoryAdminListQuery query) {

        PageBean<GoodsCategoryAdminListVO> pageBean = goodsCategoryService.getGoodsCategoryAdminList(query);
        return pageBean;
    }

    @ApiOperation("上下架状态")
    @PostMapping("/update/status")
    public Result updateCategoryStatus(@RequestParam(value = "goodsCategoryId") Long goodsCategoryId, @RequestParam(value = "status") CommonStatus status) {

        GoodsCategory updateGoodsCategory = new GoodsCategory();
        updateGoodsCategory.setId(goodsCategoryId);
        updateGoodsCategory.setStatus(status);
        goodsCategoryService.updateGoodsCategory(updateGoodsCategory);

        return ok("更改商品类别状态成功");
    }

    @ApiOperation("新增商品类别")
    @PostMapping("/do/add")
    public Result doAddGoodsCategory(@Validated GoodsCategoryAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        GoodsCategory insertGoodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(addAO, insertGoodsCategory);
        goodsCategoryService.addGoodsCategory(insertGoodsCategory);

        return ok("新增商品类别成功");
    }

    @ApiOperation("编辑商品类别")
    @PostMapping("/do/edit")
    public Result doEditGoodsCategory(@Validated GoodsCategoryEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        GoodsCategory updateGoodsCategory = new GoodsCategory();
        BeanUtils.copyProperties(editAO, updateGoodsCategory);
        updateGoodsCategory.setId(editAO.getCategoryId());
        goodsCategoryService.updateGoodsCategory(updateGoodsCategory);

        return ok("编辑商品类别成功");
    }

    @ApiOperation("全部的商品类别列表，不分页")
    @GetMapping("/get/all/list")
    public Result<List<GoodsCategory>> getAllGoodsCategoryList() {
        return ok("获取成功", goodsCategoryService.getAllGoodsCategoryList());
    }

}
